﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HRM.Web.master
{
    public partial class AddRoles : System.Web.UI.Page
    {
        #region Pageload
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetDataByRoleID();

            }
        }
        #endregion

        #region Page methods

        private void GetDataByRoleID()
        {
            if (Request.QueryString["RoleID"] != null && Request.QueryString["RoleID"] != "")
            {
                BLL.Role objRole = new BLL.Role();
                DataTable dt = objRole.GetByID(Convert.ToInt32(Request.QueryString["RoleID"]));
                if (dt.Rows.Count > 0)
                {
                    txtRoleName.Text = dt.Rows[0]["RoleName"].ToString();

                    txtDescription.Text = dt.Rows[0]["Description"].ToString();
                    hdnID.Value = dt.Rows[0]["ID"].ToString();
                }
                else
                {
                    ClearFields();
                }
            }
        }
        #endregion

        #region Button clicks and method
        private void ClearFields()
        {
            hdnID.Value = "0";
            txtRoleName.Text = "";
            txtDescription.Text = "";
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            divAlert.Visible = false;
            BO.Role BO_objRole = new BO.Role();
            BO_objRole.ID = Convert.ToInt32(hdnID.Value.Trim());
            BO_objRole.RoleName = txtRoleName.Text.Trim();
            BO_objRole.Description = txtDescription.Text.Trim();
            BLL.Role BLL_objRole = new BLL.Role();
            BLL_objRole.Save(BO_objRole);
            divAlert.Visible = true;
            if (BO_objRole.ID == 0)
            {
                //ltrMessage.Text = "This record has been saved successfully.";
            }
            else
            {
                //ltrMessage.Text = "This record has been updated successfully.";
            }
            Response.Redirect("ManageRoles.aspx" + BLL.Common.GetQueryStringValues());
            //ClearFields();

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ManageRoles.aspx" + BLL.Common.GetQueryStringValues());
        }
        #endregion
    }
}